package com.huodongrili.calender;

import android.net.http.SslError;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import com.huodongrili.calender.data.Constant;

public class LinkWebActivity extends CalenderFragmentActivity {

	private WebView mWebView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_linkweb);

		setPageTitleData(true);

		Bundle b = getIntent().getExtras();

		String link = "";

		if (b != null) {
			link = b.getString(Constant.EVENT_LINK);
		}

		mWebView = (WebView) findViewById(R.id.webview);
		
		WebSettings ws = mWebView.getSettings();
        ws.setJavaScriptEnabled(true);
        ws.setSupportZoom(true);   
        ws.setDefaultZoom(WebSettings.ZoomDensity.FAR);  
        ws.setBuiltInZoomControls(true);

		mWebView.loadUrl(link);

		final LinearLayout loadpane = (LinearLayout) findViewById(R.id.load_pane);

		mWebView.setWebChromeClient(new WebChromeClient() {
			@Override
			public void onProgressChanged(WebView view, int newProgress) {

				if (newProgress == 100) {
					loadpane.setVisibility(View.GONE);
					mWebView.setVisibility(View.VISIBLE);
				}

				super.onProgressChanged(view, newProgress);
			}
		});

		mWebView.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				mWebView.loadUrl(url);
				return true;
			}
			
			@Override
			public void onReceivedSslError(WebView view,
					SslErrorHandler handler, SslError error) {
				
				handler.proceed();
				
			}
			
		});

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
			mWebView.goBack();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
